package cn.edu.zjgsu.ito.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.zjgsu.ito.dao.UserInfomationMapper;
import cn.edu.zjgsu.ito.dao.UserScoreMapper;
import cn.edu.zjgsu.ito.dao.UserVoucherViewMapper;
import cn.edu.zjgsu.ito.dao.UserVouchersMapper;
import cn.edu.zjgsu.ito.dao.VouchersMapper;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserScore;
import cn.edu.zjgsu.ito.model.UserVoucherView;
import cn.edu.zjgsu.ito.model.UserVoucherViewExample;
import cn.edu.zjgsu.ito.model.UserVouchers;
import cn.edu.zjgsu.ito.model.UserVouchersExample;
import cn.edu.zjgsu.ito.model.Vouchers;
import cn.edu.zjgsu.ito.model.VouchersExample;
import cn.edu.zjgsu.ito.service.UserWalletManageServiceI;
import cn.edu.zjgsu.ito.util.GetTime;
import cn.edu.zjgsu.ito.util.WalletStateConstant;
import cn.edu.zjgsu.ito.vo.UserInfomationShowVo;
import cn.edu.zjgsu.ito.vo.VouchersIdVo;
import cn.edu.zjgsu.ito.vo.VouchersShowVo;

@Service("userWalletManageService")
public class UserWalletManageServiceImpl implements UserWalletManageServiceI {

	@Autowired
	private UserVouchersMapper userVouchersMapper;
	
	@Autowired
	private UserVoucherViewMapper userVoucherViewMapper;
	
	@Autowired
	private VouchersMapper vouchersMapper;
	
	@Autowired
	private UserInfomationMapper userInfomationMapper;
	
	@Autowired
	private UserScoreMapper userScoreMapper;
	
	@Override
	public int lookVouchersSize(int userId) {

		UserVouchersExample example = new UserVouchersExample();
		example.or().andUserIdEqualTo(userId);
		example.or().andUserVouchersStateEqualTo(WalletStateConstant.USER_VOUCHERS_USABLE);
		List<UserVouchers> userVouchers = userVouchersMapper.selectByExample(example);
		
		return userVouchers.size();
	}

	@Override
	public List<VouchersShowVo> lookOwnVouchers(int userId) {

		List<VouchersShowVo> vouchersShowVos = new ArrayList<VouchersShowVo>();

		UserVoucherViewExample userVoucherViewExample = new UserVoucherViewExample();
		userVoucherViewExample.or().andUserIdEqualTo(userId);
		userVoucherViewExample.or().andUserVouchersStateEqualTo(WalletStateConstant.USER_VOUCHERS_USABLE);
		List<UserVoucherView> userVoucherViews = userVoucherViewMapper.selectByExample(userVoucherViewExample);
		for(int i = 0;i < userVoucherViews.size();i++){
			VouchersShowVo vouchersShowVo = new VouchersShowVo();
			BeanUtils.copyProperties(userVoucherViews.get(i), vouchersShowVo);
			vouchersShowVo.setVouchersFailuretime(GetTime.dateToStr(userVoucherViews.get(i).getVouchersFailuretime()));
			vouchersShowVos.add(vouchersShowVo);
		}

		return vouchersShowVos;
	}

	@Override
	public List<VouchersShowVo> exchangeVouchersStore() {
		List<VouchersShowVo> vouchersShowVos = new ArrayList<VouchersShowVo>();
		
		VouchersExample example = new VouchersExample();
		example.or().andVouchersStateEqualTo(WalletStateConstant.VOUCHERS_USABLE);
		List<Vouchers> vouchers = vouchersMapper.selectByExample(example);
		for (int i = 0; i < vouchers.size(); i++) {
			VouchersShowVo vouchersShowVo = new VouchersShowVo();
			BeanUtils.copyProperties(vouchers.get(i), vouchersShowVo);
			vouchersShowVo.setVouchersFailuretime(GetTime.dateToStr(vouchers.get(i).getVouchersFailuretime()));
			vouchersShowVos.add(vouchersShowVo);
		}
		return vouchersShowVos;
	}

	@Override
	public UserInfomationShowVo userInfomation(int userId) {

		UserInfomationShowVo userInfomationShowVo = new UserInfomationShowVo();
		UserInfomation userInfomation = userInfomationMapper.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userInfomation, userInfomationShowVo);
		
		UserScore userScore = userScoreMapper.selectByPrimaryKey(userId);
		BeanUtils.copyProperties(userScore, userInfomationShowVo);
		return userInfomationShowVo;
	}

	@Override
	public int exchangeVouchers(VouchersIdVo vouchersIdVo, int userId) {
		
		UserScore userScore = userScoreMapper.selectByPrimaryKey(userId);
		int integralScore = userScore.getIntegralScore();//自己的
		
		Vouchers vouchers = vouchersMapper.selectByPrimaryKey(vouchersIdVo.getVouchersId());
		int vouchersScore = vouchers.getVouchersScore();//需要的
		
		if(integralScore > vouchersScore){
			
			UserVouchers userVouchers = new UserVouchers();
			userVouchers.setUserId(userId);
			userVouchers.setVouchersId(vouchersIdVo.getVouchersId());
			userVouchers.setUserVouchersState(WalletStateConstant.USER_VOUCHERS_USABLE);
			userVouchersMapper.insert(userVouchers);
			return integralScore - vouchersScore;
		}else{
			return integralScore;
		}
	}
	
	

}
